<?php
  /**
  Author: shielamarie
  Date: Oct 2011
  **/
	class StudentController
	{
		private $d;
		public function __construct()
		{
			$this->d = new DbHelper();
		}
		public function create($student)
		{
			$i = $student['id'];
			$n = $student['name'];
			$c = $student['section'];
			$p = $student['payment'];
			$s = "INSERT INTO Students VALUES ('$i', '$n', '$c', '$p')";
			return $this->d->query($s);
		}
		public function read($studentId)
		{
			$s = "SELECT * FROM Students WHERE StudentId = $studentId";
			return $this->d->query($s);
		}
		public function update($student)
		{
			$i = $student['id'];
			$n = $student['name'];
			$c = $student['section'];
			$b = $student['balance'];
			$p = $student['payment'];
			$remainingBalance = (int)$b - (int)$p;
			$s = "UPDATE Students SET Id='$i', Name='$n', Section='$c', " .
					"Balance='$remainingBalance' WHERE Id = $i";
			$this->d->query($s);
		}
		public function updatePassword($password)
		{
		  session_start();
		  $u = $_SESSION['loggedUser'];
		  $uid = $u['UserId'];
		  //
		  $query = "UPDATE Users SET Password= '$password' WHERE UserId='$uid'";
		  $this->d->query($query);
		}
		public function delete($studentId)
		{
			$s = "DELETE FROM Students WHERE Id = $studentId";
			$this->d->query($s);
		}
		public final function getRemainingBalance($id)
		{
		  require_once('AdminController.php');
		  $a = new AdminController();
      //
		  $logs = $a->getPaymentLogsByStudent($id);
		  $sum = 0;
		  //get student total fees
		  $sDetails = mysql_fetch_assoc($this->read($id));
		  $total = $sDetails['TotalFees'];
		  //
		  while($rows = mysql_fetch_assoc($logs))
		  {
		    $sum+=$rows['PaymentAmount'];
		  }
		  //Compute remaining balance.
		  $balance = $total - $sum;

		  return $balance;
		}
		public final function getStudentIsEnroled($id, $schoolYear)
		{
			$sy = "(SELECT Id FROM SchoolYears WHERE Year = $schoolYear)";
			$s = "SELECT COUNT(Id) FROM Students WHERE Id = $studentId AND
					SchoolYearId = $sy";
			$i = mysql_result($this->d->query($s), 0);
			return $i > 0;
		}
	}
?>